//
//  AELLicenseParticularsController.swift
//  ActualExaminationLine
//
//  Created by iOS-Czz on 2023/10/29.
//

import UIKit
import WMZDialog
class AELLicenseParticularsController: AELBaseViewController {
    var vehicleType = 0
    @IBOutlet var vehicleTypeLabel: UILabel!
    @IBOutlet var tableView: UITableView!
    @IBOutlet var licenseNumLabel: UILabel!
    lazy var viewModel: AELPointsViewModel = {
        return AELPointsViewModel()
    }()
    @IBOutlet var residueLabel: UILabel!
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setTab()
        self.vehicleType = UserDefaults.standard.value(forKey: "VehicleType") as! Int
        self.vehicleTypeLabel.text = Vehicle_Type[self.vehicleType]
        self.licenseNumLabel.text = UserDefaults.standard.value(forKey: "DrivingLicenseNumber") as? String
        // Do any additional setup after loading the view. DrivingLicenseNumber
    }
    func setTab(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(nibName: "AELLicenseParticularsCell", bundle: .main), forCellReuseIdentifier: "AELLicenseParticularsCell")
    }
    func loadData(){
        self.viewModel.getPageData() { models in
            self.residueLabel.text = "\(self.viewModel.residuePoints)"
            self.tableView.reloadData()
        }
    }
    @IBAction func clickAddPointRecordAction(_ sender: Any) {
        self.navigationController?.pushViewController(AELNewDivingPointsController(), animated: true)
    }
    @IBAction func clickChangeType(_ sender: UIButton) {
        if sender.tag == 100 {
            if self.vehicleType > 0 {
                self.vehicleType -= 1
            }
        }else{
            if self.vehicleType < Vehicle_Type.count - 1{
                self.vehicleType += 1
            }
        }
        self.vehicleTypeLabel.text = Vehicle_Type[self.vehicleType]
        UserDefaults.standard.setValue(self.vehicleType, forKey: "VehicleType")
    }
    @IBAction func clickEditNumberAction(_ sender: Any) {
        let dialog = Dialog()
        dialog.wTypeSet()(DialogTypeWrite)
            .wMessageSet()("enter license numeber")
            .wPlaceholderSet()("enter license numeber")
            .wWirteTextMaxLineSet()(1)
            .wWirteTextMaxNumSet()(15)
            .wWriteDefaultTextSet()(self.licenseNumLabel.text!)
            .wTitleSet()("Edit")
            .wOKTitleSet()("OK")
            .wEventOKFinishSet()({ [self](anyId:Any?,otherData:Any?)in
                self.licenseNumLabel.text = anyId as? String
                UserDefaults.standard.setValue(self.licenseNumLabel.text, forKey: "DrivingLicenseNumber")
            })
            _ = dialog.wStart()
    }
    
}
extension AELLicenseParticularsController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:AELLicenseParticularsCell = tableView.dequeueReusableCell(withIdentifier: "AELLicenseParticularsCell") as! AELLicenseParticularsCell
        cell.model = self.viewModel.dataSource[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
}
